<template>
  <div class="login-box">
    <div class="login-head">登 录</div>
    <div class="login-form">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <span class="label">手机号</span>
        <el-form-item prop="phone">
          <el-input @keyup.native.13="submitLogin('ruleForm')" v-model="ruleForm.phone" placeholder="请输入已注册的易撰账号"></el-input>
        </el-form-item>
        <span class="label" style="margin-top: 20px;">密码<span class="forget" @click="reset = true; type = 2;">忘记密码?</span></span>
        <el-form-item prop="password">
          <el-input @keyup.native.13="submitLogin('ruleForm')" type="password" v-model="ruleForm.password" placeholder="请输入易撰密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitLogin('ruleForm')" style="width: 100%; margin-top: 20px; margin-bottom: 20px;">登 录</el-button>
        </el-form-item>
      </el-form>
    </div>
    <span class="register" @click="reset = true; type = 1;">没有易撰账号？立即注册</span>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      isCreate: false,
      ruleForm: {
        phone: '',
        password: ''
      },
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      reset: false,
      type: 1
    }
  },
  created () {
    this.isCreate = this.$route.query.new === 'true'
    // 是否记住密码
    // let cok = remenber.getCookie('remenber')
    // if (cok) {
    // let form = JSON.parse(remenber.JSDecrypt(cok))
    // this.ruleForm.phone = form.userName
    // this.ruleForm.password = form.userPwd
    // } else {
    //   this.ruleForm.phone = ''
    //   this.ruleForm.password = ''
    // }
  },
  methods: {
    submitLogin (formName) { // BPM系统直接用易撰账号登录
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.post(this.api.LoginYz, {
            userName: this.ruleForm.phone,
            userPwd: this.ruleForm.password,
            Remember: false
          }).then(res => {
            if (res.Code === 0 || res.Code === -102) {
              this.CheckCompany()
            } else {
              this.$message.error(res.Msg)
            }
          })
        } else {
          return false
        }
      })
    },
    CheckCompany (tel) { // 如果是第一次登录，跳转到完善公司信息页，否则跳转到管理密码登录页
      let phone = this.ruleForm.phone
      if (tel) phone = tel
      this.$http.get(this.api.CheckCompany).then(res => {
        if (res.Code === 0) {
          this.$router.push({ path: '/enter', query: { isCreate: this.isCreate } })
        } else if (res.Code === -1) {
          this.$router.push({ path: '/addInfo', query: { phone: phone } })
        } else {
          this.$message.error(res.Msg)
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-box {
  position: relative;
  width: 380px;
  background: #fff;
  border-radius: 6px;
  margin: 0 auto;
  margin-top: 60px;
  padding-bottom: 30px;
  .login-head {
    font-size: 16px;
    color: #333;
    text-align: center;
    line-height: 50px;
    border-bottom: 1px solid #DEDEDE;
  }
  .login-form {
    padding: 20px 60px 10px 0;
    .label {
      display: block;
      padding-left: 60px;
      font-size: 12px;
      color: #666;
      margin-bottom: 10px;
    }
    .el-form-item {
      margin-bottom: 0;
    }
    .el-form-item__content {
      margin-left: 60px !important;
    }
    .btn-master1 {
      width: 100%;
      height: 40px;
      border-radius: 4px;
      margin-top: 5px;
    }
    .forget {
      font-size: 12px;
      color: #999;
      float: right;
      cursor: pointer;
      user-select: none;
      &:hover {
        color: $color-a;
      }
    }
  }
  .tips {
    font-size: 12px;
    color: $color-del;
    text-align: center;
  }
  .register {
    display: block;
    text-align: center;
    font-size: 12px;
    color: $color-a;
    cursor: pointer;
    user-select: none;
  }
}
</style>
